// import 'whatwg-fetch';
// import { Toast } from 'antd-mobile';

// import { store } from 'src/store/index';

// import { INCREMENTREQUESTTIMES, DECREASEREQUESTTIMES } from './../constants/common';

// 请求计数器
// let count = 0
export const request = (
  url = '',
  option: RequestInit = {},
  needLoading = true,
  contentType = 'application/json; charset=UTF-8'
) => {


  return new Promise(async (resolve: (obj: any) => void) => {
    const controller = new AbortController();
    const signal = controller.signal;
    let timer = setTimeout(() => {
      controller.abort();
   
    }, 60000);

    const token = localStorage.getItem('AUTHORIZATION');

    let headers;
    if (!contentType) {
      headers = {
        Authorization: token ? 'Bearer ' + token : '',
      };
    } else {
      headers = {
        'Content-Type': contentType,
      };
    }

    fetch(`/dev-api${url}`, {
      credentials: 'include',
      method: 'get',
      headers: headers,
      signal,
      ...option,
    }).then(
      async (response: Response) => {
        clearTimeout(timer);
       
        try {
          const json = (await response.json()) ?? {};
          resolve(json);
        } catch (err) {
          console.log('Toast.fail', err);

         
        }
      },
      (err: Error) => {
        clearTimeout(timer);
        if (needLoading) {
       
        }
        console.log('Toast.fail', err);
       
      }
    );
  });
};
